package cn.sofwin.day0116;

public class Test02 {
    public static void main(String[] args) {
        int[][] numbers = {{2,4,1},{4,9,2,3}};
        display(numbers);
        System.out.println("最大值为：" + max(numbers));
        System.out.println("最小值为：" + min(numbers));
        System.out.println("平均数为：" + avg(numbers));
        swap(numbers,0,1,1,3);
        System.out.println("交换后的数组为");
        display(numbers);

    }

    /**
     * 交换二维数组中的两个元素
     * @param array
     * @param i0
     * @param j0
     * @param i1
     * @param j1
     */
    static void swap(int[][] array, int i0, int j0, int i1, int j1){
        int temp = array[i0][j0];
        array[i0][j0] = array[i1][j1];
        array[i1][j1] = temp;
    }


    /**
     * 求二维数组中的平均数
     * @param array
     * @return
     */
    static int avg(int[][] array) {
        int sum = 0;
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                sum += array[i][j];

            }
            count += array[i].length;

        }
        sum /= count;
        return sum;
    }

    /**
     * 求二维数组中的最小数
     * @param array
     * @return
     */
        static int min(int[][] array){
            int n = array[0][0];
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    n = array[i][j] < n ? array[i][j] : n;
                }


            }
            return n;
        }

    /**
     * 求二维数组中的最大数
     * @param array
     * @return
     */
    static int max(int[][] array){
        int n = array[0][0];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                n = array[i][j] > n ? array[i][j] : n;
            }
        }

        return n;
    }

    /**
     * 打印二维数组
     * @param array
     */
    static void display(int[][] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]);
                if(j < array[i].length - 1){
                    System.out.print(",\t");
                }

            }
            System.out.println("");
        }

    }
}
